package xyx.chenweb.myapp.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xyx.chenweb.myapp.service.ApiService;
import xyx.chenweb.myapp.utils.GenerateVerifyCode.RealizeVerifyCode;
import xyx.chenweb.myapp.utils.GenerateVerifyCode.VerifyCode;
import xyx.chenweb.myapp.utils.MyResponseData;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 免登录的类
 */
@Controller
@RequestMapping("/chen/api")
public class ApiController {

    @Autowired
    private ApiService apiService;

    /**
     * 获取验证码
     * @param response
     */
    @GetMapping("/getVerificationCode")
    public void intVerificationCode(HttpServletResponse response){
        RealizeVerifyCode realizeVerifyCode = new RealizeVerifyCode();
        Subject subject = SecurityUtils.getSubject();

        try{
            Session session = subject.getSession();
            VerifyCode verifyCode = realizeVerifyCode.generate(80, 30);
            String code = verifyCode.getCode();
            session.setAttribute("code", code);

            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 用户登录
     * @param account
     * @param password
     * @param verifyCode
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public MyResponseData login(@RequestParam("account")String account, @RequestParam("password")String password,
                                @RequestParam("verifyCode")String verifyCode){
        return apiService.login(account, password, verifyCode);
    }

    /**
     * 用户注册
     * @param phone
     * @param name
     * @param pwd
     * @param verifyCode
     * @param sex
     * @param birthday
     * @param email
     * @param addr
     * @param qq
     * @param wx
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public MyResponseData register(@RequestParam("phone") String phone, @RequestParam("name")String name, @RequestParam("pwd")String pwd,
                                   @RequestParam("verifyCode")String verifyCode, @RequestParam("sex") String sex, @RequestParam("birthday") String birthday,
                                   @RequestParam("email")String email, @RequestParam("addr")String addr, @RequestParam("qq")String qq,
                                   @RequestParam("wx")String wx){
        return apiService.register(phone, name, pwd, verifyCode, sex, birthday, email, addr,qq, wx);
    }

    /**
     * 用户退出登录
     * @return
     */
    @GetMapping("/logout")
    @ResponseBody
    public MyResponseData logout(){
        return apiService.logout();
    }

}
